<?php
/**
 * 后台页面模板更新脚本
 * 用于快速更新所有后台页面的移动端自适应布局
 */

session_start();
require_once '../classes/Database.php';
require_once '../classes/SettingsHelper.php';

// 检查管理员登录
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

// 要更新的文件列表
$files = [
    'cdks.php',
    'orders.php',
    'payments.php',
    'settings.php'
];

$results = [];

foreach ($files as $file) {
    if (!file_exists($file)) {
        $results[$file] = '文件不存在';
        continue;
    }
    
    $content = file_get_contents($file);
    
    // 替换头部CSS和样式
    $content = preg_replace(
        '/<style>[\s\S]*?<\/style>/',
        '<link href="assets/admin.css" rel="stylesheet">',
        $content
    );
    
    // 替换侧边栏结构
    $content = preg_replace(
        '/<!-- 侧边栏 -->[\s\S]*?<!-- 主内容区 -->/',
        '<!-- 顶部导航栏（移动端显示） -->
    <div class="top-navbar d-flex align-items-center justify-content-between">
        <button class="sidebar-toggle">
            <i class="fas fa-bars"></i>
        </button>
        <h5 class="mb-0">' . getPageTitle($file) . ' - <?php echo htmlspecialchars(getSiteName()); ?></h5>
        <div class="text-muted small">
            <i class="fas fa-user me-1"></i><?php echo htmlspecialchars($_SESSION[\'admin_username\']); ?>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="sidebar-header">
            <h4 class="mb-3">
                <i class="fas fa-cogs me-2"></i><span><?php echo htmlspecialchars(getSiteName()); ?></span>
            </h4>
            <p class="text-muted mb-0 small">欢迎, <?php echo htmlspecialchars($_SESSION[\'admin_username\']); ?></p>
        </div>
        
        <nav class="nav flex-column">
            <a href="index.php" class="nav-link" data-tooltip="仪表盘">
                <i class="fas fa-tachometer-alt"></i><span>仪表盘</span>
            </a>
            <a href="products.php" class="nav-link" data-tooltip="商品管理">
                <i class="fas fa-box"></i><span>商品管理</span>
            </a>
            <a href="cdks.php" class="nav-link" data-tooltip="CDK管理">
                <i class="fas fa-gift"></i><span>CDK管理</span>
            </a>
            <a href="orders.php" class="nav-link" data-tooltip="订单管理">
                <i class="fas fa-shopping-cart"></i><span>订单管理</span>
            </a>
            <a href="payments.php" class="nav-link" data-tooltip="支付记录">
                <i class="fas fa-credit-card"></i><span>支付记录</span>
            </a>
            <a href="settings.php" class="nav-link" data-tooltip="系统设置">
                <i class="fas fa-cog"></i><span>系统设置</span>
            </a>
            <a href="logout.php" class="nav-link" data-tooltip="退出登录">
                <i class="fas fa-sign-out-alt"></i><span>退出登录</span>
            </a>
        </nav>
    </div>

    <!-- 主内容区 -->',
        $content
    );
    
    // 设置当前页面激活状态
    $currentPage = str_replace('.php', '', $file);
    $content = str_replace(
        'href="' . $file . '" class="nav-link"',
        'href="' . $file . '" class="nav-link active"',
        $content
    );
    
    // 添加JavaScript文件
    if (strpos($content, 'assets/admin.js') === false) {
        $content = str_replace(
            '</body>',
            '    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/admin.js"></script>
</body>',
            $content
        );
    }
    
    // 保存文件
    if (file_put_contents($file, $content)) {
        $results[$file] = '更新成功';
    } else {
        $results[$file] = '更新失败';
    }
}

function getPageTitle($file) {
    $titles = [
        'cdks.php' => 'CDK管理',
        'orders.php' => '订单管理',
        'payments.php' => '支付记录',
        'settings.php' => '系统设置'
    ];
    return $titles[$file] ?? '后台管理';
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模板更新 - <?php echo htmlspecialchars(getSiteName()); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="assets/admin.css" rel="stylesheet">
</head>
<body>
    <!-- 顶部导航栏（移动端显示） -->
    <div class="top-navbar d-flex align-items-center justify-content-between">
        <button class="sidebar-toggle">
            <i class="fas fa-bars"></i>
        </button>
        <h5 class="mb-0">模板更新 - <?php echo htmlspecialchars(getSiteName()); ?></h5>
        <div class="text-muted small">
            <i class="fas fa-user me-1"></i><?php echo htmlspecialchars($_SESSION['admin_username']); ?>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="sidebar-header">
            <h4 class="mb-3">
                <i class="fas fa-cogs me-2"></i><span><?php echo htmlspecialchars(getSiteName()); ?></span>
            </h4>
            <p class="text-muted mb-0 small">欢迎, <?php echo htmlspecialchars($_SESSION['admin_username']); ?></p>
        </div>
        
        <nav class="nav flex-column">
            <a href="index.php" class="nav-link" data-tooltip="仪表盘">
                <i class="fas fa-tachometer-alt"></i><span>仪表盘</span>
            </a>
            <a href="products.php" class="nav-link" data-tooltip="商品管理">
                <i class="fas fa-box"></i><span>商品管理</span>
            </a>
            <a href="cdks.php" class="nav-link" data-tooltip="CDK管理">
                <i class="fas fa-gift"></i><span>CDK管理</span>
            </a>
            <a href="orders.php" class="nav-link" data-tooltip="订单管理">
                <i class="fas fa-shopping-cart"></i><span>订单管理</span>
            </a>
            <a href="payments.php" class="nav-link" data-tooltip="支付记录">
                <i class="fas fa-credit-card"></i><span>支付记录</span>
            </a>
            <a href="settings.php" class="nav-link" data-tooltip="系统设置">
                <i class="fas fa-cog"></i><span>系统设置</span>
            </a>
            <a href="logout.php" class="nav-link" data-tooltip="退出登录">
                <i class="fas fa-sign-out-alt"></i><span>退出登录</span>
            </a>
        </nav>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>
                <i class="fas fa-sync-alt me-2"></i>模板更新结果
            </h2>
            <a href="index.php" class="btn btn-primary">
                <i class="fas fa-arrow-left me-2"></i>返回仪表盘
            </a>
        </div>

        <div class="card">
            <div class="card-header">
                <h4 class="mb-0">更新结果</h4>
            </div>
            <div class="card-body">
                <table class="table">
                    <thead>
                        <tr>
                            <th>文件</th>
                            <th>状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($results as $file => $status): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($file); ?></td>
                            <td>
                                <span class="badge bg-<?php echo $status === '更新成功' ? 'success' : 'danger'; ?>">
                                    <?php echo htmlspecialchars($status); ?>
                                </span>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                
                <div class="alert alert-info mt-3">
                    <h5><i class="fas fa-info-circle me-2"></i>说明</h5>
                    <p>此脚本已自动更新后台页面的布局，添加了移动端自适应和侧边栏收缩功能。</p>
                    <p>主要更新内容：</p>
                    <ul>
                        <li>添加了移动端顶部导航栏</li>
                        <li>实现了侧边栏收缩功能</li>
                        <li>优化了移动端响应式布局</li>
                        <li>添加了工具提示和动画效果</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/admin.js"></script>
</body>
</html>
